<template>
  <el-dialog
    v-model="dialogVisible"
    title="生码"
    width="800px"
    @close="closeDialog"
  >
    <el-form label-position="left" label-width="100px" :model="formLabelAlign">
      <el-form-item label="存货编码">
        <el-input
          v-model="state.formInfo.cInvCode"
          :disabled="true"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item label="存货名称">
        <el-input
          v-model="state.formInfo.cInvName"
          :disabled="true"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item label="总数量">
        <el-input-number
          v-model="state.formInfo.qualifiedQty"
          :disabled="true"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item label="生成个数">
        <el-input-number
          v-model="state.formInfo.num"
          :min="1"
          :precision="0"
          style="width: 80%"
          @change="numChange"
        />
      </el-form-item>
      <el-form-item label="是否合并" :span="12">
        <el-switch
          v-model="state.formInfo.isHb"
          active-text="是"
          :active-value="true"
          inactive-text="否"
          :inactive-value="false"
          inline-prompt
          size="small"
          @change="isHbChange"
        />
      </el-form-item>
    </el-form>
    <el-table :data="state.kbr" highlight-current-row>
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="条码数量" prop="qty" width="300">
        <template #default="scope">
          <el-input v-model="scope.row.qty" />
        </template>
      </el-table-column>
      <el-table-column label="批次号" prop="invbatch">
        <template #default="scope">
          <el-input
            v-model="scope.row.invbatch"
            placeholder="批次可自动生成！"
          />
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="save">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
  import { ref } from 'vue'
  import { localStorageConfig } from '@/config/localstorage.config'
  import { addCgdhBarcodeApi } from '@/api/wms/caigou'
  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const $baseMessage = inject('$baseMessage')
  const dialogVisible = ref(false)
  const state = reactive({
    pkCorp: pkCorp,
    formInfo: {},
    kbr: [],
  })
  const openAddBarDialog = (row) => {
    state.formInfo = row
    dialogVisible.value = true
    state.formInfo.isHb = false
    state.formInfo.num = 1
    if (row.needCheck) {
      state.formInfo.qualifiedQty = row.qualifiedQty
    } else {
      state.formInfo.qualifiedQty = row.iQuantity
    }
    state.formInfo.pkCorp = pkCorp
    state.formInfo.kcBarcodeRecords = []
    createBarCode()
  }

  const isHbChange = () => {
    createBarCode()
  }
  const numChange = () => {
    createBarCode()
  }
  const createBarCode = () => {
    state.kbr = []
    let lastQty = state.formInfo.qualifiedQty % state.formInfo.num
    let qty = Math.floor(state.formInfo.qualifiedQty / state.formInfo.num)
    for (let i = 0; i < state.formInfo.num; i++) {
      state.kbr.push({ qty: qty, invbatch: '' })
    }
    if (lastQty > 0) {
      if (state.formInfo.isHb) {
        state.kbr[state.kbr.length - 1].qty =
          state.kbr[state.kbr.length - 1].qty + lastQty
      } else {
        state.kbr.push({ qty: lastQty, invbatch: '' })
      }
    }
  }

  const save = () => {
    if (state.formInfo.num && state.kbr.length > 0) {
      ElMessageBox.confirm('确定进行到货生码吗?', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          // const load = $baseLoading()
          // try {
          state.formInfo.kcBarcodeRecords = state.kbr
          const { code, msg } = await addCgdhBarcodeApi({ ...state.formInfo })
          if (code === 2000) {
            $baseMessage(msg, 'success')
            dialogVisible.value = false
          } else {
            $baseMessage(msg, 'error')
          }
          // } finally {
          //   load.close()
          // }
        })
        .catch(() => {})
    } else {
      $baseMessage('请填写到货码个数！', 'warning')
    }
  }
  const closeDialog = () => {
    dialogVisible.value = false
  }
  defineExpose({ openAddBarDialog })
</script>
